blocking
A thread pool for isolating blocking I/O in async programs.
Sometimes there's no way to avoid blocking I/O. Consider files or stdin, which have weak async support on modern operating systems. While IOCP, AIO, and io_uring are possible solutions, they're not always available or ideal.
Since blocking is not allowed inside futures, we must move blocking I/O onto a special thread pool provided by this crate. The pool dynamically spawns and stops threads depending on the current number of running I/O jobs.
Note that there is a limit on the number of active threads. Once that limit is hit, a running job has to finish before others get a chance to run. When a thread is idle, it waits for the next job or shuts down after a certain timeout.
The default number of threads (set to 500) can be altered by setting BLOCKING_MAX_THREADS environment variable with value between 1 and 10000.
Examples
Read the contents of a file:
use unblock;
use fs;
let contents = unblock.await?;
println!;
Read a file and pipe its contents to stdout:
use ;
use io;
use File;
let input = unblock.await?;
let input = new;
let mut output = new;
copy.await?;
Iterate over the contents of a directory:
use Unblock;
use *;
use fs;
let mut dir = new;
while let Some = dir.next.await
Spawn a process:
use unblock;
use Command;
let out = unblock.await?;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.